/*
 * Created on 18-ene-2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package hotel.client;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;

import hotel.interfaces.*;
import java.util.*;

/**
 * @author marble
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Test1 {
	
	String nomHotel = null;
	Hotel hotel = null;
	
	/** Home interface */
	protected hotel.interfaces.HotelHome home;
	/**
	 * Get the initial naming context
	 */
	protected Context getInitialContext() throws Exception {
		Hashtable props = new Hashtable();
		props.put(
			Context.INITIAL_CONTEXT_FACTORY,
			"org.jnp.interfaces.NamingContextFactory");
		props.put(
			Context.URL_PKG_PREFIXES,
			"org.jboss.naming:org.jnp.interfaces");
		props.put(Context.PROVIDER_URL, "jnp://localhost:1099");
		Context ctx = new InitialContext(props);
		return ctx;
	}

	/**
	 * Get the home interface
	 */
	protected hotel.interfaces.HotelHome getHome() throws Exception {
		Context ctx = this.getInitialContext();
		Object o = ctx.lookup("ejb/Hotel");
		hotel.interfaces.HotelHome intf = (hotel.interfaces.HotelHome) PortableRemoteObject
			.narrow(o, hotel.interfaces.HotelHome.class);
		return intf;
	}
	
	public void menu() {
		System.out.println(nomHotel);
		System.out.println("0. Mostrar aquest menu");
		System.out.println("1. Alta de Client");
		System.out.println("2. Baixa de Client");
		System.out.println("3. Llistat de Clients");
		System.out.println("4. Alta d'habitacio");
		System.out.println("5. Baixa d'habitacio");
		System.out.println("6. Llistat d'habitacions");
		System.out.println("7. Reservar");
		System.out.println("8. Anul·lar reserva");
		System.out.println("9. Llistar reserves");
		System.out.println("10. Sortir");
		System.out.println();
	}
	
	public void llistatClients() throws Exception {
		Collection c = hotel.llistatClients();
		Iterator it = c.iterator();
		while (it.hasNext()) {
			ClientValue client = (ClientValue)it.next();
			System.out.println("- " + client.getDNI() + " :: " + client.getNom());
		}
	}
	
	public void llistatHabitacions() throws Exception {
		Collection c = hotel.llistatHabitacions();
		Iterator it = c.iterator();
		while (it.hasNext()) {
			HabitacioValue habitacio = (HabitacioValue)it.next();
			System.out.println("- " + habitacio.getID() + " :: Tipus " + habitacio.getTipus());
		}
	}
	
	public void llistatReserves() throws Exception {
		Collection c = hotel.llistatReserves();
		Iterator it = c.iterator();
		while (it.hasNext()) {
			ReservaValue reserves = (ReservaValue)it.next();
			System.out.println("- " + reserves.getID() + " Dates: " + reserves.getDesdeData() + " :: " + reserves.getFinsData());
		}
	}
	
	public void altaClient() throws Exception {
		BufferedReader entrada = new BufferedReader(new InputStreamReader(System.in));
		
		int DNI = 0;
		boolean repeat;
		do {
			repeat = false;
			System.out.print("DNI: ");
			try {
				String sDNI = entrada.readLine();
				DNI = Integer.parseInt(sDNI);
			} catch (Exception e) {
				System.out.println("Nomes número.");
				repeat = true;
			}
		} while (repeat);
		System.out.print("Nom: ");
		String nom = entrada.readLine();
		hotel.altaClient(new Integer(DNI), nom);
	}
	
	public void baixaClient() throws Exception {
		BufferedReader entrada = new BufferedReader(new InputStreamReader(System.in));
		
		int DNI = 0;
		boolean repeat;
		do {
			repeat = false;
			System.out.print("DNI: ");
			try {
				String sDNI = entrada.readLine();
				DNI = Integer.parseInt(sDNI);
			} catch (Exception e) {
				System.out.println("Nomes número.");
				repeat = true;
			}
		} while (repeat);
		hotel.baixaClient(new Integer(DNI));
	}
	
	public void habilitarHabitacio() throws Exception {
		BufferedReader entrada = new BufferedReader(new InputStreamReader(System.in));
		
		int ID = 0;
		int tipus = 0;
		boolean repeat;
		do {
			repeat = false;
			System.out.print("ID: ");
			try {
				String sID = entrada.readLine();
				ID = Integer.parseInt(sID);
			} catch (Exception e) {
				System.out.println("Nomes número.");
				repeat = true;
			}
		} while (repeat);
		do {
			repeat = false;
			System.out.print("Tipus: ");
			try {
				String sTipus = entrada.readLine();
				tipus = Integer.parseInt(sTipus);
			} catch (Exception e) {
				System.out.println("Nomes número.");
				repeat = true;
			}
		} while (repeat);
		hotel.habilitarHabitacio(new Integer(ID), new Integer(tipus));
	}
	
	public void deshabilitarHabitacio() throws Exception {
		BufferedReader entrada = new BufferedReader(new InputStreamReader(System.in));
		
		int ID = 0;
		boolean repeat;
		do {
			repeat = false;
			System.out.print("DNI: ");
			try {
				String sID = entrada.readLine();
				ID = Integer.parseInt(sID);
			} catch (Exception e) {
				System.out.println("Nomes número.");
				repeat = true;
			}
		} while (repeat);
		hotel.baixaClient(new Integer(ID));
	}
	
	public void reservar() throws Exception {
		BufferedReader entrada = new BufferedReader(new InputStreamReader(System.in));
		SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
		
		int ID = 0;
		int clientDNI = 0;
		int habitacioID = 0;
		Date desde = null;
		Date fins = null;
		boolean repeat;
		do {
			repeat = false;
			System.out.print("ID: ");
			try {
				String sID = entrada.readLine();
				ID = Integer.parseInt(sID);
			} catch (Exception e) {
				System.out.println("Nomes número.");
				repeat = true;
			}
		} while (repeat);
		do {
			repeat = false;
			System.out.print("DNI Client: ");
			try {
				String sID = entrada.readLine();
				clientDNI = Integer.parseInt(sID);
			} catch (Exception e) {
				System.out.println("Nomes número.");
				repeat = true;
			}
		} while (repeat);
		do {
			repeat = false;
			System.out.print("ID Habitacio: ");
			try {
				String sHabitacio = entrada.readLine();
				habitacioID = Integer.parseInt(sHabitacio);
			} catch (Exception e) {
				System.out.println("Nomes número.");
				repeat = true;
			}
		} while (repeat);
		
		do {
			repeat = false;
			System.out.print("Des de data (DD/MM/YYYY): ");
			try {
				desde = formatter.parse(entrada.readLine());
			} catch (Exception e) {
				System.out.println("Format DD/MM/YYYY, e.g. 23/06/1981");
				repeat = true;
			}
		} while (repeat);
		
		do {
			repeat = false;
			System.out.print("Fins data (DD/MM/YYYY): ");
			try {
				fins = formatter.parse(entrada.readLine());
			} catch (Exception e) {
				System.out.println("Format DD/MM/YYYY, e.g. 23/06/1981");
				repeat = true;
			}
		} while (repeat);
		
		Vector vHabitacions = new Vector();
		vHabitacions.add(new Integer(ID));
		hotel.reservar(new Integer(ID), new Integer(clientDNI), vHabitacions, desde, fins);
	}
	
	public void anularReserva() throws Exception {
		BufferedReader entrada = new BufferedReader(new InputStreamReader(System.in));
		
		int ID = 0;
		boolean repeat;
		do {
			repeat = false;
			System.out.print("ID Reserva: ");
			try {
				String sID = entrada.readLine();
				ID = Integer.parseInt(sID);
			} catch (Exception e) {
				System.out.println("Nomes número.");
				repeat = true;
			}
		} while (repeat);
		hotel.anularReserva(new Integer(ID));
	}
	
	public Test1 () throws Exception {
		BufferedReader entrada = new BufferedReader(new InputStreamReader(System.in));
		
		this.home = this.getHome();
		this.hotel = home.create();
		this.nomHotel = hotel.getNom();
		
		menu();
		
		int accio = 0;
		do {
			try {
				String linia = entrada.readLine();
				try {
					accio = Integer.parseInt(linia);
				} catch (Exception e) {
					System.out.println("Opcio no valida");
				}
				
				switch(accio) {
					case 0:
						menu();
						break;
					case 1:
						altaClient();
						break;
					case 2:
						baixaClient();
						break;
					case 3:
						llistatClients();
						break;
					case 4:
						habilitarHabitacio();
						break;
					case 5:
						deshabilitarHabitacio();
						break;
					case 6:
						llistatHabitacions();
						break;
					case 7:
						reservar();
						break;
					case 8:
						anularReserva();
						break;
					case 9:
						llistatReserves();
						break;
				}
			}catch(Exception ex) {
				System.out.println("ERROR: " + ex.getMessage());
				ex.printStackTrace();
			}
		} while (accio != 10);
			
	}

	public static void main(String[] args) throws Exception{
		new Test1();
	}
}
